<%@ page import="com.MusicWeb.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../components/base.jsp" %>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <title>主页</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            text-decoration: none;
        }

        #all {
            width: 1000px;
            height: 600px;
            margin: 0 auto;
            /*overflow: scroll;*/
            overflow-y: auto;
            background-color: white;
            border: 3px solid #4e88a3;
            opacity: 0.9;
            border-radius: 0 0 15px 15px;
        }

        #musicpage {
            width: 1000px;
            padding: 20px;
            text-align: center;

        }

        .music-th th {
            width: 50%;
            display: inline-block;
        }

        .music-th-id {
            text-align: center;
            width: 62px;
        }

        #musicpage th {
            height: 40px;
            line-height: 40px;
            background-color: #beb8ca;
        }

        #musicpage td {
            height: 30px;
            line-height: 30px;
        }

        #musicpage a {
            color: black;
        }

        #musicpage a:hover {
            text-decoration: underline;
        }

        tr:nth-child(even) {
            background: #dcdcdc;
        }

        tr:nth-child(odd) {
            background: #ffffff;

        }

        /*回复内容*/
        #replyContent {
            resize: none;
        }

        #userR {
            position: absolute;
            left: 500px;
            display: inline-block;
            padding-bottom: 10px;
            vertical-align: middle;
            background-color: rgba(255, 255, 255,1);
            border-radius: 4px;
            border: 1px solid #ebeef5;
            font-size: 18px;
            box-shadow: 0 2px 12px 0 #a1a1a1;
            text-align: left;
            overflow: hidden;
            backface-visibility: hidden;

        }

        #userR table{
            margin: 0 auto;
        }

        #userR input {
            width: 280px;
            cursor: pointer;
            background-color: #fff;
            background-image: none;
            border-radius: 4px;
            border: 1px solid #dcdfe6;
            box-sizing: border-box;
            color: #606266;
            display: inline-block;
            font-size: inherit;
            height: 28px;
            line-height: 28px;
            outline: none;
            padding: 0 15px;
            transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
        }

        #userR button{
            width: 100px;
            margin-bottom: 15px;

            line-height: 1;
            white-space: nowrap;
            cursor: pointer;
            background: #fff;
            border: 1px solid #dcdfe6;
            color: #606266;
            text-align: center;
            box-sizing: border-box;
            outline: none;
            transition: .1s;
            font-weight: 500;
            height: 40px;
            line-height: 40px;
            font-size: 18px;
            border-radius: 4px;
        }

        #userR button:hover{
            color: #fff;
            background-color: #409eff;
            border-color: #409eff;
        }

        #userR textarea{
            width: 348px;
            margin-left: 25px;
        }

        .button{
            margin-left: 100px;
        }

    </style>

</head>
<%--通用代码，每个页面都引入--%>
<%@include file="../components/header.jsp" %>
<%@ include file="../components/base.jsp" %>
<body>
<div id="all">
    <h1 style="padding: 20px">歌曲列表</h1>

    <%
        User user = (User) request.getSession().getAttribute("user");
        Integer u_id = user.getId();
    %>

    <input id="user" type="text" value="<%= u_id %>" style="display: none">

    <%--    用户回复上传人--%>
    <div id="userR" style="">

    </div>
    <%--    显示歌曲--%>
    <table id="musicpage">

    </table>

</div>
</body>
<script>

    // 获取用户ID
    var uid = $('#user').val();

    // 显示所有歌曲
    let list = [];

    //获取全部歌曲表单
    function Musicpage() {
        $.ajax({
            type: "GET",
            url: `http://localhost:8080/music/column`,
            dataType: "json",
            success: function (res) {
                list = res;
                if (res != null) {
                    $(`#musicpage`).html(`<tr>
                        <th class="music-th-id"></th>
                        <th class="music-th">标题</th>
                        <th class="music-th">操作</th>
                        <th class="music-th" onclick="">歌手</th>
                        <th class="music-th-id">上传者</th>
                    </tr>`);
                    for (var i = 0; i < list.length; i++) {
                        $(`#musicpage`).append(`<tr>
                            <td id="tdcolor" class="music-th-id">${i+1}</td>
                            <td id="tdcolor" class="music-th">
                                <a href="javascript:;">${list[i].music_name}</a>
                            </td>
                            <td id="tdcolor" class="operation">
                                <a href="javascript:void(0);" class="iconfont" onclick="LoveMusic(${list[i].id},${uid})">&#xe69c;</a>
                            </td>
                            <td id="tdcolor" class="music-th">${list[i].music_singer}</td>
                            <td>
                                <a id="username" href="javascript:;" onclick="userReplyYes('${list[i].user_name}',${list[i].id},${list[i].uid})">${list[i].user_name}</a>
                            </td>
                            </tr>`)
                    }
                }
            }
        });
    }

    Musicpage();

    //点击音乐播发
    function Musicplay(musicurl) {
        console.log(musicurl);
    }


    // 用户添加喜欢的音乐
    function LoveMusic(MusicID, UserID) {
        $.ajax({
            type: "GET",
            url: `http://localhost:8080/CollectionAdd?music_id=${MusicID}&user_id=${UserID}`,
            dataType: "text",
            success: function (res) {
                console.log(res)
                if (res==1){
                    alert("收藏歌曲成功！")
                    location.reload();
                }else {
                    alert("收藏歌曲重复！")
                }

            }
        });

    }


    // 用户回复上传人
    function userReply(name, mid, uid) {
        var username = name;
        $('#userR').html(`
             <form action="<%=request.getContextPath()%>/tipadd" method="post">
                <h2 style="padding: 20px;text-align: center;">回复消息</h2>
                <div id="information" style="width: 400px; margin: 0 auto;">
                    <table>
                        <tr>
                            <th> 用户名：</th>
                            <th><input type="text" value="${username}" readonly="readonly"></th>
                        </tr>
                        <tr>
                            <th> 标题：</th>
                            <th><input name="title" type="text"></th>
                        </tr>
                    </table>
                    <input name="mid" type="text" value="${mid}" style="display: none;" readonly>
                    <input name="uid" type="text" value="${uid}" style="display: none;" readonly>
                     <input name="from_id" type="text" value="<%=u_id%>" style="display: none;" readonly>

                    <textarea name="text" id="replyContent" cols="30" rows="10"></textarea>
                    <br><br>

                    <div class="button">
                        <button type="submit">回复</button>
                        <a onclick="userReplyNot()">取消</a>
                    </div>

                </div>
            </form>
        `);
    }


    // 回复
    function userReplyYes(name, mid, uid) {
        $('#userR').css('display', 'block');
        var names = name;
        userReply(names, mid, uid);
    }

    // 取消回复
    function userReplyNot() {
        $('#userR').css('display', 'none');
    }


</script>
</html>
